package net.maku.biz.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import net.maku.framework.common.utils.PageResult;
import net.maku.framework.common.utils.Result;
import net.maku.biz.convert.BizDemocracyConvert;
import net.maku.biz.entity.BizDemocracyEntity;
import net.maku.biz.service.BizDemocracyService;
import net.maku.biz.query.BizDemocracyQuery;
import net.maku.biz.vo.BizDemocracyVO;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import jakarta.validation.Valid;
import java.util.List;

/**
* 民主管理
*
* @author 阿沐 babamu@126.com
* @since 1.0.0 2025-03-27
*/
@RestController
@RequestMapping("biz/biz_democracy")
@Tag(name="民主管理")
@AllArgsConstructor
public class BizDemocracyController {
    private final BizDemocracyService bizDemocracyService;

    @GetMapping("page")
    @Operation(summary = "分页")
    @PreAuthorize("hasAuthority('biz:biz_democracy:page')")
    public Result<PageResult<BizDemocracyVO>> page(@ParameterObject @Valid BizDemocracyQuery query){
        PageResult<BizDemocracyVO> page = bizDemocracyService.page(query);

        return Result.ok(page);
    }

    @GetMapping("{id}")
    @Operation(summary = "信息")
    @PreAuthorize("hasAuthority('biz:biz_democracy:info')")
    public Result<BizDemocracyVO> get(@PathVariable("id") Long id){
        BizDemocracyEntity entity = bizDemocracyService.getById(id);

        return Result.ok(BizDemocracyConvert.INSTANCE.convert(entity));
    }

    @PostMapping
    @Operation(summary = "保存")
    @PreAuthorize("hasAuthority('biz:biz_democracy:save')")
    public Result<String> save(@RequestBody BizDemocracyVO vo){
        bizDemocracyService.save(vo);

        return Result.ok();
    }

    @PutMapping
    @Operation(summary = "修改")
    @PreAuthorize("hasAuthority('biz:biz_democracy:update')")
    public Result<String> update(@RequestBody @Valid BizDemocracyVO vo){
        bizDemocracyService.update(vo);

        return Result.ok();
    }

    @DeleteMapping
    @Operation(summary = "删除")
    @PreAuthorize("hasAuthority('biz:biz_democracy:delete')")
    public Result<String> delete(@RequestBody List<Long> idList){
        bizDemocracyService.delete(idList);

        return Result.ok();
    }

    @GetMapping("unmount/{id}/{status}")
    @Operation(summary = "上下架")
    @PreAuthorize("hasAuthority('biz:biz_democracy:unmount')")
    public Result<String> unmount(@PathVariable("id") Long id, @PathVariable("status") Integer status){
        bizDemocracyService.unmount(id,status);

        return Result.ok();
    }
}